package com.GoldMiner.util;

import com.GoldMiner.ui.GameMain;

import java.awt.*;

import static com.GoldMiner.util.Img.hook;

/**
 * 矿工的线
 */
public class Line {
    private int x = 390;//起点x坐标
    private int y = 85;//起点y坐标
    private int endX = 600;//终点x坐标
    private int endY = 30;//终点y坐标
    private double length = 80;//线的长度
    private int direction = 1;//线的方向，1为正方向，-1为负方向
    private double n = 0;
    private int state;//线的状态，0为摇摆，1为抓取，2为没有抓到物体收回，3为金块碰撞完成
    GameMain gameMain;

    public Line(GameMain gameMain) {
        //空参构造函数
        this.gameMain = gameMain;
    }

    /**
     * 重写paint方法，绘制界面上的各种图形。
     *
     * @param g Graphics对象，用于在组件上进行绘制。
     */
    public void paint(Graphics g) {
        logic();//金块碰撞逻辑
        //通过状态画线
        switch (state) {
            case 0: //摇摆
                if (n < 0.1) {
                    direction = 1; //未超过180度，正方向
                } else if (n > 0.9) {
                    direction = -1; //超过180度，反方向
                }
                n += 0.005 * direction;//角度转弧度
                lines(g);//画线
                break;
            case 1: //抓取
                if (length < 550) {
                    length += 10; //线长增加
                    lines(g);
                } else {
                    state = 2; //抓取完成，收回线
                }
                break;
            case 2: //收回
                if (length > 80) {
                    length -= 10; //线长减少
                    lines(g);
                } else {
                    state = 0; //收回线,摇摆
                }
                break;
            case 3: //金块碰撞完成
                int m = 1; //暂停时间
                if (length > 80) {
                    length -= 10; //线长减少
                    //画线
                    lines(g);
                    for (Item item : this.gameMain.items) {
                        //是否为被抓取的金块
                        if (item.isIsCollected()) {
                            m = item.getM(); //暂停时间
                            //将金块位置设置为跟着线移动
                            item.setX(endX - item.getWidth() / 2);
                            item.setY(endY);
                            if (length <= 100) {
                                //将金块移出屏幕外
                                item.setX(-150);
                                item.setY(-150);
                                item.setIsCollected(false);//移动完毕修改回来
                                //增加积分
                                GameMain.count += item.getCount();
                                state = 0; //收回线,摇摆
                            }
                        }

                    }
                }
                try {
                    Thread.sleep(m);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            default: //摇摆
                if (n < 0.1) {
                    direction = 1; //未超过180度，正方向
                } else if (n > 0.9) {
                    direction = -1; //超过180度，反方向
                }
                n += 0.005 * direction;//角度转弧度
                lines(g);
                break;

        }
    }

    /**
     * 计算线的坐标并绘制
     */
    private void lines(Graphics g) {
        //公式计算终点坐标
        endX = (int) (x + length * Math.cos(n * Math.PI));
        endY = (int) (y + length * Math.sin(n * Math.PI));
        g.setColor(Color.black);
        g.drawLine(x, y, endX, endY);
        //加粗
        g.drawLine(x-1, y, endX-1, endY);
        g.drawLine(x+1, y, endX+1, endY);
        g.drawImage(hook,endX-25,endY-2,null);
    }

    /**
     * 金块碰撞的逻辑
     */
    public void logic() {
        //逻辑处理
        for (Item item : this.gameMain.items){
            if (endX > item.getX() && endX < item.getX() + item.getWidth() && endY > item.getY() && endY < item.getY() + item.getHeight()) {
                //物体碰撞
                setState(3);//抓取状态返回3
                //System.out.println(endX + " " + endY + " " + item.getX() + " " + item.getY() + " " + item.getWidth() + " " + item.getHeight());
                item.setIsCollected(true);
                break;
            }
        }
    }

    public void update(Graphics g){
        //更新线的坐标
        paint(g);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getEndX() {
        return endX;
    }

    public void setEndX(int endX) {
        this.endX = endX;
    }

    public int getEndY() {
        return endY;
    }

    public void setEndY(int endY) {
        this.endY = endY;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public double getN() {
        return n;
    }

    public void setN(double n) {
        this.n = n;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String toString() {
        return "Line{x = " + x + ", y = " + y + ", endX = " + endX + ", endY = " + endY + ", length = " + length + ", direction = " + direction + ", n = " + n + ", state = " + state + "}";
    }
}
